#include "Widget.h"
#include "ui_Widget.h"
#include<QKeyEvent>
#include<QTimer>
#include<QPushButton>
#include<QToolButton>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    this->setFixedSize(1024,1024);

    mGameView.setSceneRect(QRect(0,0,1024,1024));
    mGameScene.setSceneRect(QRect(0,0,1024,1024));//游戏进行场景
    mStartScene.setSceneRect(QRect(0,0,1024,1024));//游戏开始场景
    mStartScene.addPixmap(QPixmap(":/start/images/startscene.png"));

    auto startBtn=new QToolButton();
    startBtn->setAutoRaise(true);
    startBtn->resize(400,100);
    startBtn->setIcon(QIcon(":/start/images/startbutton.png"));
    startBtn->setIconSize(QSize(400,100));
    startBtn->move(724,700);
    connect(startBtn,&QToolButton::clicked,[this](){
        mGameView.setScene(&mGameScene);
        mGameView.show();
    });


    mStartScene.addWidget(startBtn);

    mbackground.setPixmap(QPixmap(":/background/images/background.png"));
    mmyplayer.setPixmap(QPixmap(":/player/images/player.png"));

    //图片元素添加到场景
    mGameScene.addItem(&mbackground);
    mGameScene.addItem(&mmyplayer);

    //设置视图场景
    mGameView.setScene(&mGameScene);
    mGameView.setScene(&mStartScene);
    //设置视图的父亲为窗口
    mGameView.setParent(this);
    mGameView.show();


    //开启篮子移动检测定时器
    mplayerMoveTimer = new QTimer(this);
    mplayerMoveTimer->start(10);
    connect(mplayerMoveTimer,&QTimer::timeout,this,&Widget::playermove);

    //开启掉落物创建定时器
    mEnemyfallingCreateTimer = new QTimer(this);
    mEnemyfallingCreateTimer->start(2000);
    connect(mEnemyfallingCreateTimer,&QTimer::timeout,this,&Widget::Createfalling);

    //开启移动定时器
    mEnemyfallingMoveTimer =new QTimer(this);
    mEnemyfallingMoveTimer->start(50);
    connect(mEnemyfallingMoveTimer,&QTimer::timeout,[this](){
        for(auto enemyfalling : mEnemyfallingList)
        {
            enemyfalling->EnemyfallingMove();
        }
    });

}

Widget::~Widget()
{
    delete ui;
}

void Widget::playermove()
{
    for(int keyCode : mKeyList)
    {
        switch (keyCode)
        {
        case   Qt::Key_A:mmyplayer.moveBy(-1*mmyplayer.movespeed,0);break;
        case   Qt::Key_D:mmyplayer.moveBy(1*mmyplayer.movespeed,0);break;
        }
    }

    //边界判断
    if (mmyplayer.x()<0)
    {
        mmyplayer.setX(0);
    }
    if (mmyplayer.y()<800)
    {
        mmyplayer.setY(800);
    }
    if (mmyplayer.x()>800)
    {
        mmyplayer.setX(800);
    }
    if (mmyplayer.y()>800)
    {
        mmyplayer.setY(800);
    }
}

void Widget::keyPressEvent(QKeyEvent *event)
{
    //添加对应按键组合
    switch (event->key())
    {
        case   Qt::Key_A:
        case   Qt::Key_D:
        mKeyList.append(event->key());
        break;
    }
}

void Widget::keyReleaseEvent(QKeyEvent *event)
{
    //移除对应按键组合
    if(mKeyList.contains(event->key()))
    {
        mKeyList.removeOne(event->key());
    }
}

void Widget::Createfalling()
{
    QPixmap pixmap(":/falling/images/bomb.png");
    int randX=rand()%(800);
    Enemyfalling *enemyfalling =new Enemyfalling(QPoint(randX,-224),pixmap);

    //添加到场景
    mGameScene.addItem(enemyfalling);

    //添加到管理器
    mEnemyfallingList.append(enemyfalling);
}
